package com.demo.gateway.common.configure;

import cn.dev33.satoken.stp.StpInterface;
import com.demo.common.core.domain.LoginUser;
import com.demo.common.redis.constants.CacheConstants;
import com.demo.common.redis.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author MyPC
 */
@Component  // 保证此类被 SpringBoot 扫描，完成 Sa-Token 的自定义权限验证扩展
public class StpInterfaceImpl implements StpInterface {

    @Autowired
    private RedisService redisService;

    /**
     * 返回一个账号所拥有的权限码集合
     */
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        String userKey = CacheConstants.LOGIN_USER_KEY + loginId;
        LoginUser loginUser = redisService.getCacheObject(userKey);
        return new ArrayList<>(loginUser.getPermissions());
    }

    /**
     * 返回一个账号所拥有的角色标识集合 (权限与角色可分开校验)
     */
    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        String userKey = CacheConstants.LOGIN_USER_KEY + loginId;
        LoginUser loginUser = redisService.getCacheObject(userKey);
        return new ArrayList<>(loginUser.getRoles());
    }
}

